from django_redis import get_redis_connection

from common.utils.cache_conn import get_cart_redis_conn


def get_cart_dict(user):
    """从Redis购物车中查询出的商品信息"""

    redis_conn = get_cart_redis_conn()
    redis_cart = redis_conn.hgetall('carts_%s' % user.id)
    cart_selected = redis_conn.smembers('selected_%s' % user.id)
    cart_dict = {}
    for sku_id, count in redis_cart.items():
        cart_dict[int(sku_id)] = {
            'count': int(count),
            'selected': sku_id in cart_selected
        }
    return cart_dict


def get_cart_dict_selected(user):
    """从Redis购物车中查询出被勾选的商品信息"""

    redis_conn = get_cart_redis_conn()
    redis_cart = redis_conn.hgetall('carts_%s' % user.id)
    cart_selected = redis_conn.smembers('selected_%s' % user.id)
    cart_dict = {}
    for sku_id in cart_selected:
        cart_dict[int(sku_id)] = {
            'count': int(redis_cart[sku_id])
        }

    return cart_dict
